﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TaobaoPick.storage;

namespace TaobaoPick
{
    public partial class CategoryFilterForm : Form
    {
        MyTaobao1DataContext context = null;
        Hashtable codetoNode = new Hashtable();

        bool m_withCatFilter = false;

        public bool WithCatFilter
        {
            get { return m_withCatFilter; }
            set { m_withCatFilter = value; }
        }
        /// <summary>
        /// 取得沒有..被選的filter
        /// </summary>
        /// <returns></returns>
        public HashSet<String> getFilterCat()
        {
            HashSet<String> result = new HashSet<String>();

            foreach (String k in codetoNode.Keys)
            {
                TreeNode tn = (TreeNode)codetoNode[k];
                if (tn.Checked == false)
                {
                    result.Add(k);
                }
            }
            return result;
        }

        public CategoryFilterForm()
        {
            InitializeComponent();
            context = LinqAgent.getTaobaoContext();
            preLoadCategories();
            comboBox1.SelectedIndex = 1;
            m_withCatFilter = true;
        }

        protected void SeekCheckedItem(TreeNodeCollection nodes, List<TreeNode> result)
        {
            if (nodes.Count == 0)
                return;
            foreach (TreeNode n in nodes)
            {
                if (n.Checked)
                    result.Add(n);
                SeekCheckedItem(n.Nodes, result);
            }
        }
        protected void updateListBox()
        {
            List<TreeNode> result = new List<TreeNode>();
            SeekCheckedItem(treeView1.Nodes, result);
            listBox1.Items.Clear();
            List<String> rr = new List<string>();


            foreach (TreeNode n in result)
            {
                rr.Add(n.Text);
            }
            listBox1.Items.AddRange(rr.ToArray());
        }



        //回傳的是 沒有被選的,這樣好 
        List<Hashtable> m_preSelected = null;
        
        public void setPreSelect(List<Hashtable>  preSelected)
        {
            m_preSelected = preSelected;
            if (m_preSelected == null)
                m_preSelected = new List<Hashtable>();
            resetToDefault();
        }


        protected void decheckTree(TreeNodeCollection nodes)
        {
            foreach(TreeNode node in nodes)
            {
                if(node.Checked)
                    node.Checked=false;
                decheckTree(node.Nodes);
            }            
        }
        protected void clearTreeViewCheck()
        {
            decheckTree(treeView1.Nodes);
        }
        protected void resetToDefault()
        {
            //1.把treeview中每個都清掉
            clearTreeViewCheck(); 
            //2.把preselect的都選起來
            foreach (Hashtable ht in m_preSelected)
            {
                String cat = (String)ht["cat"];
                TreeNode tn = (TreeNode)codetoNode[cat];
                if (tn != null)
                {
                    tn.Checked = true;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        protected void preLoadCategories()
        {
            treeView1.Nodes.Clear();
            treeView1.ExpandAll();
            createCategoryTree(null, null);
        }

        protected void createCategoryTree(TreeNode parentNode, Object id)
        {
            if (id == null)
            {
                var root = (from category in context.Categories where category.parentCategory == null select category);
                foreach (var cat in root)
                {
                    TreeNode node = new TreeNode();
                    node.Text = cat.categoryName;
                    node.Tag = cat;
                    treeView1.Nodes.Add(node);
                    if (String.IsNullOrWhiteSpace(cat.categoryCode) == false)
                    {
                        codetoNode[cat.categoryCode] = node;
                    }
                    
                    createCategoryTree(node, cat.categoryId);
                }
            }
            else
            {
                Guid gid = (Guid)id;
                var root = (from category in context.Categories where category.parentCategory == gid select category);
                foreach (var cat in root)
                {
                    TreeNode node = new TreeNode();
                    node.Text = cat.categoryName;
                    if (String.IsNullOrWhiteSpace(cat.categoryCode) == false)
                    {
                        node.Text = node.Text + "(" + cat.categoryCode + ")";
                    }
                    node.Tag = cat;
                    //cat.parentCategory
                    if (String.IsNullOrWhiteSpace(cat.categoryCode) == false)
                    {
                        codetoNode[cat.categoryCode] = node;
                    }
                    parentNode.Nodes.Add(node);
                    createCategoryTree(node, cat.categoryId);
                }
            }
        }
        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            updateListBox();
        }
        
        private void button3_Click(object sender, EventArgs e)
        {
            resetToDefault();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                treeView1.Enabled = false;
                listBox1.Enabled = false;
                m_withCatFilter = false;
            }
            else
            {
                treeView1.Enabled = true;
                listBox1.Enabled = true;
                m_withCatFilter = true;
            }
        }

        private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            MessageBox.Show("char="+e.KeyChar);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Abort;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
